package me.xiaoyan.android.tuition.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.json.JSONArray;
import org.json.JSONException;
import com.unionpay.UPPayAssistEx;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;

/**
 * 银联手机支付
 * 
 * @date 2013-08-21 15:45
 * @author yancheng
 * @version 1.0.1
 */
@SuppressWarnings("unused")
public class UnionPay {

	private final static String LOG_TAG = "UionPayMobile";
	/**
	 * 运行环境 (“00” – 银联正式环境 “01” – 银联测试环境，该环境中不发生真实交易)
	 */
	private final static String PAY_MODE = "01";

	/**
	 * 支付控件可用
	 */
	private final static int PLUGIN_VALID = 0;

	/**
	 * 支付控件未安装
	 */
	private final static int PLUGIN_NOT_INSTALLED = -1;

	/**
	 * 支付控件需要升级
	 */
	private final static int PLUGIN_NEED_UPGRADE = 2;

	private String payResult = "1";

	/**
	 * 用银联交易流水号去调用支付控件完成支付
	 * 
	 * @param tnNumber
	 *            //流水号
	 */
	public void payUsTN(final String tnNumber, final Activity activity) {

		final Context c = activity.getApplicationContext();
		int ret = UPPayAssistEx.startPay(activity, null, null, tnNumber,
				PAY_MODE);
		// 没有安装支付插件或者需要升级
		if (ret == PLUGIN_NEED_UPGRADE || ret == PLUGIN_NOT_INSTALLED) {
			// 需要重新安装控件
			Log.e(LOG_TAG, "plugin not found or need upgrade!!!");

			AlertDialog.Builder builder = new AlertDialog.Builder(c);
			builder.setTitle("提示");
			builder.setMessage("完成购买需要安装银联手机支付控件，是否安装？");

			builder.setNegativeButton("确定",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							UPPayAssistEx.installUPPayPlugin(c);
						}
					});

			builder.setPositiveButton("取消",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
			builder.create().show();
		}
	}

	
}
